package com.summer.study.leetcode.thread.sequence_114;

import java.util.concurrent.CountDownLatch;

/**
 * @auther: 杨杰
 * @date: 2019/7/25 10:40
 * @description:
 */
public class CountDownLatchDemo {
    private CountDownLatch countDownLatchA;
    private CountDownLatch countDownLatchB;

    public CountDownLatchDemo() {
        countDownLatchA=new CountDownLatch(1);
        countDownLatchB=new CountDownLatch(2);
    }

    public void first(Runnable printFirst) throws InterruptedException {
        printFirst.run();
        countDownLatchA.countDown();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        countDownLatchA.await();
        printSecond.run();
        countDownLatchB.countDown();
    }

    public void third(Runnable printThird) throws InterruptedException {
        countDownLatchB.await();
        printThird.run();
    }

    public static void main(String[] args) throws InterruptedException{
        Foo foo=new Foo();
        CountDownLatchDemo demo=new CountDownLatchDemo();
        demo.first(new Thread(() -> {
            foo.one();
        }));
        demo.second(new Thread(() -> {
            foo.two();
        }));
        demo.third(new Thread(() -> {
            foo.three();
        }));
    }

}
